package xworker.gluon.glisten.actions;

import com.gluonhq.charm.glisten.control.*;
import javafx.scene.control.ButtonType;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.lang.executor.Executor;

import java.time.LocalDate;

public class GlistenActions {
    private static final String TAG = GlistenActions.class.getName();

    //xworker.gluon.glisten.actions.GlistenActions/@Alert/@actions/@run
    public static void alert(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String type = self.doAction("getType", actionContext);
        String message = self.doAction("getMessage", actionContext);

        Alert alert;
        switch (type){
            case "CONFIRMATION":
                alert = new Alert(javafx.scene.control.Alert.AlertType.CONFIRMATION, message);
                break;
            case "ERROR":
                alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR, message);
                break;
            case "WARNING":
                alert = new Alert(javafx.scene.control.Alert.AlertType.WARNING, message);
                break;
            case "INFORMATION":
                alert = new Alert(javafx.scene.control.Alert.AlertType.INFORMATION, message);
                break;
            default:
                alert = new Alert(javafx.scene.control.Alert.AlertType.NONE, message);
        }

        alert.showAndWait().ifPresent(buttonType -> {
            if(buttonType == ButtonType.OK){
                self.doAction("ok", actionContext);
            }else if(buttonType == ButtonType.CANCEL){
                self.doAction("cancel", actionContext);
            }
        });
    }

    public static void datePicker(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        LocalDate date = self.doAction("getDate", actionContext);
        DatePicker datePicker = new DatePicker();
        if(date != null){
            datePicker.setDate(date);
        }
        datePicker.showAndWait().ifPresent(localDate -> {

        });
        datePicker.showAndWait().ifPresent(localDate -> {
            self.doAction("onClosed", actionContext, "date", localDate);
        });
    }

    public static void showDialog(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Thing dialogThing = self.doAction("getDialog", actionContext);
        if(dialogThing == null){
            Executor.warn(TAG, "Can not show dialig ,dialog thing is null, thing=" + self.getMetadata().getPath());
            return;
        }

        ActionContext ac = new ActionContext();
        ac.put("parentContext", actionContext);
        Dialog<?> dialog = dialogThing.doAction("create", ac);
        dialog.showAndWait().ifPresent(result -> self.doAction("onClosed", actionContext, "result", result));
    }

    public static void showExceptionDialog(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Thing dialogThing = self.doAction("getExceptionDialog", actionContext);
        if(dialogThing == null){
            Executor.warn(TAG, "Can not show dialig ,dialog thing is null, thing=" + self.getMetadata().getPath());
            return;
        }

        ActionContext ac = new ActionContext();
        ac.put("parentContext", actionContext);
        ExceptionDialog dialog = dialogThing.doAction("create", ac);
        dialog.showAndWait();
    }

    public static void showSnackbar(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Thing thing = self.doAction("getSnackbar", actionContext);
        if(thing == null){
            Executor.warn(TAG, "thing is null, thing=" + self.getMetadata().getPath());
            return;
        }

        ActionContext ac = new ActionContext();
        ac.put("parentContext", actionContext);
        Snackbar snackbar = thing.doAction("create", ac);
        snackbar.show();
    }

    public static void showToask(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Thing thing = self.doAction("getToast", actionContext);
        if(thing == null){
            Executor.warn(TAG, "thing is null, thing=" + self.getMetadata().getPath());
            return;
        }

        ActionContext ac = new ActionContext();
        ac.put("parentContext", actionContext);
        Toast toast = thing.doAction("create", ac);
        toast.show();
    }
}
